package com.saifan.wyy_ov.ui.communitylife;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.ConvenienttooBean;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.utils.CallPhoneUtil;
import com.saifan.wyy_ov.utils.ImgUtil;
import com.saifan.wyy_ov.utils.StringUtil;
import com.squareup.picasso.Picasso;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-5-14 20:22:04
 * @descrption 便民服务
 */

public class ConvenienttooInfoActivity extends BaseActivity {
    private Toolbar toolbar;

    ConvenienttooBean convenienttooBean;
    private ImageView BMGJTPLJ;
    private TextView BMGJLXR;
    private TextView BMGJLXDH;
    private TextView BMGJDZ;
    private TextView BMGJFWSM;
    private TextView BMGJJJ;
    private ImageView signauthentication;
    private TextView BMGJBT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        convenienttooBean = (ConvenienttooBean) getIntent().getSerializableExtra("ConvenienttooBean");
        setContentView();

    }

    public void setContentView() {
        setContentView(R.layout.activity_convenienttoo_info);
        this.BMGJBT = (TextView) findViewById(R.id.BMGJ_BT);
        this.signauthentication = (ImageView) findViewById(R.id.sign_authentication);
        this.BMGJJJ = (TextView) findViewById(R.id.BMGJ_JJ);
        this.BMGJFWSM = (TextView) findViewById(R.id.BMGJ_FWSM);
        this.BMGJDZ = (TextView) findViewById(R.id.BMGJ_DZ);
        this.BMGJLXDH = (TextView) findViewById(R.id.BMGJ_LXDH);
        this.BMGJLXR = (TextView) findViewById(R.id.BMGJ_LXR);
        this.BMGJTPLJ = (ImageView) findViewById(R.id.BMGJ_TPLJ);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle(R.string.housekeeping);
        BMGJBT.setText(StringUtil.returnShow(convenienttooBean.getBMGJ_BT()));
        BMGJJJ.setText(StringUtil.returnShow(convenienttooBean.getBMGJ_JJ()));
        BMGJFWSM.setText(StringUtil.returnShow(convenienttooBean.getBMGJ_FWSM()));
        BMGJDZ.setText(StringUtil.returnShow(convenienttooBean.getBMGJ_DZ()));
        BMGJLXDH.setText(StringUtil.returnShow(convenienttooBean.getBMGJ_LXDH()));
        BMGJLXR.setText(StringUtil.returnShow(convenienttooBean.getBMGJ_LXR()));
         DisPlayImg(convenienttooBean.getBMGJ_TPLJ(), BMGJTPLJ, ConvenienttooInfoActivity.this);
        if (!convenienttooBean.isBMGJ_WYRZ()) {
            signauthentication.setVisibility(View.GONE);
        }

    }





    public   void DisPlayImg(String Url, ImageView img, Context context) {
        if (TextUtils.isEmpty(Url)){
            Picasso.with(context).load(R.drawable.pic_none)  .resize(200, 200).centerCrop().into(img);
        }else {
            Picasso.with(context).load(Api.getUrlIp()  + Url).placeholder(R.drawable.pic_loading).error(R.drawable.pic_load_fail) .resize(200, 200).centerInside().into(img);
        }


    }
    // 联系买家
    public void call(View v) {
        if (!convenienttooBean.getBMGJ_LXDH().isEmpty()) {



            if (Build.VERSION.SDK_INT >= 23) {

                int checkCallPhonePermission = ContextCompat.checkSelfPermission( this, Manifest.permission.CALL_PHONE);
                if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(ConvenienttooInfoActivity.this, new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE);
                    return;
                } else {
                    CallPhoneUtil.callPhone(ConvenienttooInfoActivity.this, convenienttooBean.getBMGJ_LXDH());


                }
            } else {

                CallPhoneUtil.callPhone(ConvenienttooInfoActivity.this, convenienttooBean.getBMGJ_LXDH());

            }
        }



    }



    private static final int CALL_PHONE = 123;
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == CALL_PHONE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                CallPhoneUtil.callPhone(ConvenienttooInfoActivity.this, convenienttooBean.getBMGJ_LXDH());


            } else {
                // Permission Denied
                Toast.makeText(this, "您禁用拨打电话权限", Toast.LENGTH_SHORT)
                        .show();

            }
        }

    }
}
